
overlapWeekStart=25;//异常的课放在25周开始
const reg = new RegExp(/\u2605/i);
var CourseResult = [];

//将“1-18”这种字符串转化为{1,2,3,4,5,...,17,18}
function weekStr2IntList(week,course_on_week) {
    let weeks = [];

    // 以-号为界分割字符串，遍历分割的字符串
    if (week.search('-') != -1) {                  //处理5-18
        let range = week.split("-");
        let start = parseInt(range[0]);
        let end = parseInt(range[1]);
        for (let i = start; i <= end; i++) {
            if (!weeks.includes(i)) {
                weeks.push(i);
                course_on_week.push(i);
            }
        }
    } else if (week.length != 0) {
        let str0 = week.split("周");         //第x
        let str1 = str0[0].split("第");
        let v = parseInt(str1[1]);
        if (!weeks.includes(v)) {
            weeks.push(v);
            course_on_week.push(v);
        }
    }
    return weeks;
}

//课程冲突检测
function weekStrOverlapDet(week,course_on_week) {
    let overlapFlag=false;
    // 以-号为界分割字符串，遍历分割的字符串
    if (week.search('-') != -1) {                  //处理5-18
        let range = week.split("-");
        let start = parseInt(range[0]);
        let end = parseInt(range[1]);
        for (let i = start; i <= end; i++) {
            if (course_on_week.includes(i)) {
                overlapFlag=true;
            }
        }
    } else if (week.length != 0) {
        let str0 = week.split("周");         //第x
        let str1 = str0[0].split("第");
        let v = parseInt(str1[1]);
        if (course_on_week.includes(v)) {
            overlapFlag=true;
        }
    }
    return overlapFlag;
}

//课程解析函数(单元格源数据,行.列)
//★通信原理 诚毅3-205 陈朝阳 8-14★单片机原理与应用 诚毅3-205 李颖 1-7
function CourseParser(data, Row, Column) {
    try {
        if (data.search('★') != -1) {                  //处理5-18
            let DataStrSplit = data.split("★");         //切割课程字符串
            let i = 1;                                    //按位访问
            let sections = [];
            switch (Row) {                                  //判断是第几节课
                case 4: sections.push({ section: 1 }, { section: 2 });
                    break;
                case 5: sections.push({ section: 3 }, { section: 4 });
                    break;
                case 6: sections.push({ section: 5 }, { section: 6 });
                    break;
                case 7: sections.push({ section: 7 }, { section: 8 });
                    break;
                case 8: sections.push({ section: 9 }, { section: 10 });
                    break;
            }
            //console.info(DataStrSplit);
            let course_on_week = [];                                //存储这个单元格哪几周有课
            while (DataStrSplit[i] != null) {
                let week=[];
                let CourseStrSplit = DataStrSplit[i].split(" ")   //用空格来切割字符串得到课程详情
                //CourseStrSplit[0]    课程名字
                //CourseStrSplit[2]    课程教室
                //CourseStrSplit[3]    上课教师
                //CourseStrSplit[4]    上课周数
                //Row                  上课节数
                //Column               周几
                console.info(CourseStrSplit);
                if(!weekStrOverlapDet(CourseStrSplit[4],course_on_week)){
                    week=weekStr2IntList(CourseStrSplit[4],course_on_week);
                }else{
                    week.push(overlapWeekStart++);
                }
                let course = {
                    name: CourseStrSplit[0],
                    position: CourseStrSplit[2],
                    teacher: CourseStrSplit[3],
                    weeks: week,
                    day: Column,
                    sections: sections,
                }
                //console.info(course)
                CourseResult.push(course)
                i++;
            }
        }
    } catch (e) {
        console.info(e);
    }

}

function scheduleHtmlParser(html) {
    //除函数名外都可编辑
    //传入的参数为上一步函数获取到的html
    //可使用正则匹配
    //可使用解析dom匹配，工具内置了$，跟jquery使用方法一样，直接用就可以了，参考：https://juejin.im/post/5ea131f76fb9a03c8122d6b9

    //-------------表结构-------------
    // 0,0 0,1 0,2
    // 1,0
    // 2,0
    // 3,0 3,1 3,2 3,3 3,4 3,5 3,6 3,7
    // 4,0 4,1 4,2 4,3 4,4 4,5 4,6 4,7
    // 5,0 5,1 5,2 5,3 5,4 5,5 5,6 5,7
    // 6,0 6,1 6,2 6,3 6,4 6,5 6,6 6,7
    // 7,0 7,1 7,2 7,3 7,4 7,5 7,6 7,7
    // 8,0 8,1 8,2 8,3 8,4 8,5 8,6 8,7
    const $ = cheerio.load(html);
    $(html).find('tbody').find('tr').each(function (Row) {          //遍历表格的行
        $(this).find('td').each(function (Column) {                 //遍历表格的列
            if (Row > 2) {                                            //跳过前两行表头
                //console.info(Row + "," + Column);          //打印(行,列)
                let CellData = $(this).text();
                if (CellData.search('★') != -1) {
                    //console.info(CellData);
                    CourseParser(CellData, Row, Column);
                }
            }
        });
    });

    if (CourseResult.length == 0) {
        //alert(TriPrompto)
        //console.info(TriPrompto);
        let sections = [];
        sections.push({ section: 1 }, { section: 2 });
        let course = {
            name: "没有获取到课表",
            position: "课表为空",
            teacher: "反馈QQ: 2591081434",
            weeks: weekStr2IntList("1-12"),
            day: 1,
            sections: sections,
        }
        //console.info(course)
        CourseResult.push(course)
    }
    //鉴于原先的parser中的sectionTimes已经被抽离成独立的函数，所以parser的返回值可以直接返回课表信息为Array类型
    return CourseResult;
}
